package paidaxing.blog.core.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import paidaxing.blog.core.dto.SystemOverviewDTO;
import paidaxing.blog.core.dto.VisitStatDTO;
import paidaxing.blog.core.entity.VisitStats;
import paidaxing.blog.core.mapper.VisitStatsMapper;
import paidaxing.blog.core.service.VisitStatsService;

import jakarta.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class VisitStatsServiceImpl implements VisitStatsService {

    @Resource
    private VisitStatsMapper visitStatsMapper;

    @Override
    public SystemOverviewDTO getSystemOverview() {
        SystemOverviewDTO overview = new SystemOverviewDTO();
        // 由于存在循环依赖，暂时无法直接查询其他模块的统计数据
        // 需要通过其他方式获取或者在controller层进行聚合
        overview.setUserCount(0);
        overview.setPostCount(0);
        overview.setCommentCount(0);
        overview.setTotalVisits(visitStatsMapper.countTotalVisits());
        overview.setTodayVisits(visitStatsMapper.countTodayVisits());
        overview.setTotalLikes(0);
        overview.setFileCount(0);
        return overview;
    }

    @Override
    public List<VisitStatDTO> getVisitStatsByDateRange(Date startDate, Date endDate) {
        List<VisitStats> visitStatsList = visitStatsMapper.queryByDateRange(startDate, endDate);
        return visitStatsList.stream().map(visitStat -> {
            VisitStatDTO dto = new VisitStatDTO();
            BeanUtils.copyProperties(visitStat, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public List<VisitStatDTO> getVisitStatsByPostId(Long postId) {
        List<VisitStats> visitStatsList = visitStatsMapper.queryByPostId(postId);
        return visitStatsList.stream().map(visitStat -> {
            VisitStatDTO dto = new VisitStatDTO();
            BeanUtils.copyProperties(visitStat, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public boolean recordVisit(Long postId) {
        // TODO: 实现访问记录逻辑
        return true;
    }

    @Override
    public Integer getTotalVisits() {
        return visitStatsMapper.countTotalVisits();
    }

    @Override
    public Integer getTodayVisits() {
        return visitStatsMapper.countTodayVisits();
    }
}